{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474 "
     ]
    }
   ],
   "source": [
    "#水仙花数\n",
    "# 153是一个3位数，且 1**3 + 5**3 + 3**3 = 153\n",
    "# 输出所有水仙花数\n",
    "# for i in range(100,1000):\n",
    "#     a=i//100\n",
    "#     b=(i//10)%10\n",
    "#     c=i%10\n",
    "#     if a**3+b**3+c**3==i:\n",
    "#         print(i,end=\" \")\n",
    "\n",
    "def is_narcissistic_number(n):\n",
    "    \"\"\"判断一个数是否是水仙花数\"\"\"\n",
    "    num_str = str(n)\n",
    "    num_len = len(num_str)\n",
    "    sum_of_powers = sum(int(digit) ** num_len for digit in num_str)\n",
    "    return sum_of_powers == n\n",
    "# True or False\n",
    "# 寻找并打印1到10000之间的所有水仙花数\n",
    "for number in range(1, 10001):\n",
    "    if is_narcissistic_number(number):\n",
    "        print(number,end=' ')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6 28 496 "
     ]
    }
   ],
   "source": [
    "#完数\n",
    "# 所有正除数（除了它本身以外的约数）之和\n",
    "# 6的正除数是1、2、3，而1+2+3=6。\n",
    "for i in range(1,1000):\n",
    "    sum=0\n",
    "    for j in range(1,i):\n",
    "        if i%j==0:\n",
    "            sum+=j\n",
    "    if sum==i:\n",
    "        print(i,end=' ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 1 2 3 5 8 13 21 34 55 89 "
     ]
    }
   ],
   "source": [
    "def fib(n):\n",
    "    fib_sequence = [0, 1]  # 初始化斐波那契数列的前两个数字\n",
    "    while len(fib_sequence) < n:\n",
    "        fib_sequence.append(fib_sequence[-1] + fib_sequence[-2])  # 生成下一个斐波那契数\n",
    "    return fib_sequence\n",
    "\n",
    "# 打印1到100之间的斐波那契数列\n",
    "fib_sequence = fib(100)\n",
    "for number in fib_sequence[1:]:\n",
    "    if number < 100:\n",
    "        print(number, end=' ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 "
     ]
    }
   ],
   "source": [
    "#质数\n",
    "for i in range(2,100):\n",
    "    for j in range(2,i):\n",
    "        if i%j==0:\n",
    "            break\n",
    "    else:\n",
    "        print(i,end=\" \")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pytorch1",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
